﻿using Newtonsoft.Json.Linq;
using System;
using UnityEngine;

namespace BToolkit
{
    public class CloudServerAPI
    {
        private static CloudServerAPI instance;
        public static CloudServerAPI Instance { get { return instance ?? (instance = new CloudServerAPI()); } }

        public const string ServerUrl = "http://192.168.1.200:8080";
#if UNITY_ANDROID
        public static string VersionConfigPath { get { return ServerUrl + "/appdownload/androidversion.json"; } }
#elif UNITY_IOS
        public static string VersionConfigPath { get { return ServerUrl + "/appdownload/iosversion.json"; } }
#endif

        /// <summary>
        /// 根据识别的TargetId读取对象详细信息
        /// </summary>
        public void LoadTargetInfo(string targetId, Action<ARVideoInfo> Callback)
        {
            if (string.IsNullOrEmpty(targetId))
            {
                Debuger.LogError("targetId为空");
                return;
            }
            string url = ServerUrl + "/app/getVideoInfo/" + targetId;
            Http.Get(url, (int code, string result) =>
            {
                //Debug.Log("<color=yellow>>>>>>>>>>>>["+ url + "] : " + resultType + " result: " + result + "</color>");
                switch (code)
                {
                    case -1:
                        DialogAlert.Show("", "请将手机连接网络!", 2f);
                        Callback(null);
                        break;
                    case 1:
                        DialogAlert.Show("", "读取数据失败!", 2f);
                        Callback(null);
                        break;
                    case 0:
                        JObject jsonData = JObject.Parse(result);
                        int _code = (int)jsonData["code"];
                        if (_code == 0)
                        {
                            JObject data = jsonData["data"] as JObject;
                            ARVideoInfo info = new ARVideoInfo();
                            info.videoUrl = (string)data["videoUrl"];
                            Callback(info);
                        }
                        else
                        {
                            if (!DialogAlert.isShowing)
                            {
                                DialogAlert.Show("", (string)jsonData["msg"], 2f);
                            }
                            Callback(null);
                        }
                        break;
                }
            });
        }
    }
}